package com.shenfa.util;

import java.io.*;
import java.util.ArrayList;

import org.apache.log4j.Logger;

public class SysRun {
	private static Logger log = Logger.getLogger(SysRun.class);

	public static String run(boolean needReturn, String commandInput,
			String workPath, String... command) throws Exception {
		OutputStream output = null;
		InputStream input = null;
		ByteArrayOutputStream inputBuffer = null;

		try {
			ProcessBuilder processBuilder = new ProcessBuilder(command);
			if (workPath != null) {
				processBuilder.directory(new File(workPath));
			}
			processBuilder.redirectErrorStream(true);

			Process process = processBuilder.start();
			output = process.getOutputStream();
			input = process.getInputStream();

			if (commandInput != null) {
				output.write(commandInput.getBytes());
			}

			byte[] inputByte = new byte[1024];
			if (needReturn) {
				inputBuffer = new ByteArrayOutputStream(2048);
			}
			int readLen = -1;
			readLen = input.read(inputByte);
			while (readLen != -1) {
				if (needReturn) {
					inputBuffer.write(inputByte, 0, readLen);
				}
				readLen = input.read(inputByte);
			}

			if (inputBuffer != null) {
				return inputBuffer.toString();
			} else {
				return null;
			}

		} finally {
			try {
				if (output != null) {
					output.close();
				}
			} catch (Exception eClose) {
				log.error("关闭时错误", eClose);
			}
			try {
				if (input != null) {
					input.close();
				}
			} catch (Exception eClose) {
				log.error("关闭时错误", eClose);
			}
			try {
				if (inputBuffer != null) {
					inputBuffer.close();
				}
			} catch (Exception eClose) {
				log.error("关闭时错误", eClose);
			}

		}
	}

	public static ArrayList<String> run(String commandInput, String workPath,
			String... command) throws Exception {
		OutputStream output = null;
		InputStream input = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader inputBuffer = null;

		try {
			ProcessBuilder processBuilder = new ProcessBuilder(command);
			if (workPath != null) {
				processBuilder.directory(new File(workPath));
			}
			processBuilder.redirectErrorStream(true);

			Process process = processBuilder.start();
			output = process.getOutputStream();
			input = process.getInputStream();

			if (commandInput != null) {
				output.write(commandInput.getBytes());
			}

			inputStreamReader = new InputStreamReader(input);
			inputBuffer = new BufferedReader(inputStreamReader);
			String line = null;
			ArrayList<String> lines = new ArrayList<String>();
			while ((line = inputBuffer.readLine()) != null) {
				lines.add(line);
			}

			return lines;

		} finally {
			try {
				if (output != null) {
					output.close();
				}
			} catch (Exception eClose) {
				log.error("关闭时错误", eClose);
			}
			try {
				if (inputBuffer != null) {
					inputBuffer.close();
				}
			} catch (Exception eClose) {
				log.error("关闭时错误", eClose);
			}
			try {
				if (inputStreamReader != null) {
					inputStreamReader.close();
				}
			} catch (Exception eClose) {
				log.error("关闭时错误", eClose);
			}
			try {
				if (input != null) {
					input.close();
				}
			} catch (Exception eClose) {
				log.error("关闭时错误", eClose);
			}

		}
	}

	/**
	 * 删除目录
	 * 
	 * @param fullDirPath
	 *            目录全路径
	 * @throws Exception
	 */
	public static void deleteDirectory(String fullDirPath) throws Exception {
		run(false, null, null, "/usr/bin/sh", "-c", new StringBuffer("rm -fr ")
				.append(fullDirPath).toString());
	}

	/**
	 * 改变权限
	 * 
	 * @param fullDirPath
	 *            目录全路径
	 * @throws Exception
	 */
	public static void chmod(String fullDirPath) throws Exception {
		log.debug("chmod 777 " + fullDirPath);
		run(false, null, null, "/usr/bin/sh", "-c", new StringBuffer(
				"chmod 777").append(" ").append(fullDirPath).toString());
	}
	
	
	/**
	 * 改变权限
	 * 
	 * @param fullDirPath
	 *            目录全路径
	 * @throws Exception
	 */
	public static void chmod(String cmd,String fullDirPath) throws Exception {
		log.debug("cmd" + fullDirPath);
		run(false, null, null, "/usr/bin/sh", "-c", new StringBuffer(
				cmd).append(" ").append(fullDirPath).toString());
	}

	/**
	 * 文件拷贝
	 * 
	 * @param fullDirPath
	 * @throws Exception
	 */
	public static void fileCopy(String sourcePath, String targetPath)
			throws Exception {
		run(false, null, null, "/usr/bin/sh", "-c", new StringBuffer("cp -R ")
				.append(sourcePath).append(" ").append(targetPath).toString());
	}

	/**
	 * 文件tar
	 * 
	 * @param tarPath
	 *            tar目录全路径
	 * @param targetPath
	 *            需要tar的文件
	 * @throws Exception
	 */
	public static void fileTar(String tarPath, String targetPath)
			throws Exception {
		log.debug("tar -cvf \n " + targetPath + "\n " + tarPath);
		run(false, null, null, "/usr/bin/sh", "-c", new StringBuffer(
				"tar -cvf ").append(tarPath).append(" ").append(targetPath)
				.toString());

	}

	/**
	 * cmd
	 * 
	 * @param cmd
	 *            tar命令
	 * @throws Exception
	 */
	public static void cmd(String cmd) throws Exception {
		log.debug(cmd);
		run(false, null, null, "/usr/bin/sh", "-c", new StringBuffer(cmd)
				.toString());
	}

	/**
	 * cmd
	 * 
	 * @param cmd
	 *            tar命令
	 * @throws Exception
	 */
	public static String cmd2(String cmd) throws Exception {
		log.debug(cmd);
		return run(true, null, null, "/usr/bin/sh", "-c", new StringBuffer(cmd)
				.toString());
	}

	/**
	 * 文件拷贝
	 * 
	 * @param tarPath
	 *            目录全路径
	 * @param targetPath
	 * @throws Exception
	 */
	public static String query() throws Exception {
		String returnMsg = run(true, null, null, "/usr/bin/sh", "-c",
				" ps -ef |grep netserver");
		System.err.println("===============>>" + returnMsg);
		log.info(returnMsg);
		return returnMsg;

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			String runReturn = SysRun.run(true, null, "c:\\", "cmd", "/C", "dir");
			System.out.println(runReturn);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
